<?php
namespace app\daohang\event;

class Sql
{
    /**
    * 安装时触发
    * @return bool 只有返回true时才会往下执行
    */
	public function install()
    {
        //批量添加配置
        model('daohang/Datas','loglic')->insertConfig();
        
        //批量添加路由
        model('daohang/Datas','loglic')->insertRoute();
        
        //批量添加权限
        model('daohang/Datas','loglic')->insertAuth();
        
        //批量添加导航
        model('daohang/Datas','loglic')->insertNavs();
        
        //批量添加分类
        model('daohang/Datas','loglic')->insertCategory();
        
        //数据验证
        $this->insertValidate('common/Op', 'save', '');
        
        return true;
	}
    
    /**
    * 升级时触发
    * @return bool 只有返回true时才会往下执行
    */
    public function upgrade()
    {
        return true;
    }
    
    /**
    * 卸载插件时触发
    * @return bool 只有返回true时才会往下执行
    */
    public function remove()
    {
        return $this->unInstall();
    }
    
    /**
    * 删除插件时触发
    * @return bool 只有返回true时才会往下执行
    */
    public function unInstall()
    {
        //删除配置
        \daicuo\Op::delete_module('daohang');
        //删除分类
        \daicuo\Term::delete_module('daohang');
        //删除用户
        \daicuo\User::delete_module('daohang');
        //返回结果
        return true;
	}
    
    //批量添加初始配置
    private function insertConfig()
    {
        return \daicuo\Op::write([
            'theme'             => 'default',
            'theme_wap'         => 'default',
            'search_interval'   => 5,
            'page_size'         => 30,
            'index_title'       => '免费开源PHP导航系统－[siteName]',
            'index_keywords'    => 'PHP导航系统,免费导航系统,开源导航系统',
            'index_description' => '呆错导航系统是一款免费开源的分类导航建站系统，安全、便捷、高效、是您的建站首选。',
            'rewrite_index'     => '/',
            'rewrite_page'      => 'page/[:slug]/[:pageNumber]',
            'rewrite_category'  => 'category/[:slug]/[:pageNumber]',
            'rewrite_tag'       => 'tag/[:slug]/[:pageNumber]',
            'rewrite_search'    => 'search/[:searchText]/[:pageNumber]',
            'rewrite_web'       => 'site/[:slug]',
        ], 'daohang', 'system', 'config', '0', 'yes');
    }
    
    //批量添加路由
    private function insertRoute()
    {
        return \daicuo\Route::save_all([
            [
                'rule'        => '/',
                'address'     => 'daohang/index/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'system',
                'op_action'   => 'route',
            ],
            [
                'rule'        => 'category/[:slug]/[:pageNumber]',
                'address'     => 'daohang/category/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'system',
                'op_action'   => 'route',
            ],
            [
                'rule'        => 'tag/[:slug]/[:pageNumber]',
                'address'     => 'daohang/tag/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'system',
                'op_action'   => 'route',
            ],
            [
                'rule'        => 'search/[:searchText]/[:pageNumber]',
                'address'     => 'daohang/search/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'system',
                'op_action'   => 'route',
            ],
            [
                'rule'        => 'site/[:slug]',
                'address'     => 'daohang/web/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'system',
                'op_action'   => 'route',
            ],
            [
                'rule'        => 'page/[:slug]/[:pageNumber]',
                'address'     => 'daohang/page/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'system',
                'op_action'   => 'route',
            ],
        ]);
    }
    
    //批量添加导航
    private function insertNavbar()
    {
        return \daicuo\Nav::save_all([
            [
                'nav_text'        =>'首页',
                'nav_type'        =>'addon',
                'nav_module'      =>'daohang',
                'nav_controll'    =>'index',
                'nav_action'      =>'index',
                'nav_params'      =>'',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daohangindexindex',
                'op_module'       =>'daohang',
                'op_controll'     =>'navbar',
                'op_action'       =>'header',
                'op_order'        =>101,
            ],
            [
                'nav_text'        =>'分类导航',
                'nav_type'        =>'addon',
                'nav_module'      =>'daohang',
                'nav_controll'    =>'page',
                'nav_action'      =>'index',
                'nav_params'      =>'slug=category',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daohangpagecategory',
                'op_module'       =>'daohang',
                'op_controll'     =>'navbar',
                'op_action'       =>'header',
                'op_order'        =>102,
            ],
            [
                'nav_text'        =>'标签导航',
                'nav_type'        =>'addon',
                'nav_module'      =>'daohang',
                'nav_controll'    =>'page',
                'nav_action'      =>'index',
                'nav_params'      =>'slug=tag',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daohangpagetag',
                'op_module'       =>'daohang',
                'op_controll'     =>'navbar',
                'op_action'       =>'header',
                'op_order'        =>103,
            ],
            [
                'nav_text'        =>'最新收录',
                'nav_type'        =>'addon',
                'nav_module'      =>'daohang',
                'nav_controll'    =>'page',
                'nav_action'      =>'index',
                'nav_params'      =>'slug=new',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daohangpagenew',
                'op_module'       =>'daohang',
                'op_controll'     =>'navbar',
                'op_action'       =>'header',
                'op_order'        =>104,
            ],
            [
                'nav_text'        =>'排行榜',
                'nav_type'        =>'addon',
                'nav_module'      =>'daohang',
                'nav_controll'    =>'page',
                'nav_action'      =>'index',
                'nav_params'      =>'slug=top',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daohangpagetop',
                'op_module'       =>'daohang',
                'op_controll'     =>'navbar',
                'op_action'       =>'header',
                'op_order'        =>105,
            ],
            [
                'nav_text'        =>'申请收录',
                'nav_type'        =>'addon',
                'nav_module'      =>'daohang',
                'nav_controll'    =>'publish',
                'nav_action'      =>'index',
                'nav_params'      =>'',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daohangpublishindex',
                'op_module'       =>'daohang',
                'op_controll'     =>'navbar',
                'op_action'       =>'header',
                'op_order'        =>106,
            ],
            [
                'nav_text'        =>'发布网址',
                'nav_type'        =>'addon',
                'nav_module'      =>'daohang',
                'nav_controll'    =>'user',
                'nav_action'      =>'web',
                'nav_params'      =>'',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daohanguserweb',
                'op_module'       =>'daohang',
                'op_controll'     =>'navs',
                'op_action'       =>'left',
                'op_order'        =>953,
            ],
            [
                'nav_text'        =>'我的投稿',
                'nav_type'        =>'addon',
                'nav_module'      =>'daohang',
                'nav_controll'    =>'user',
                'nav_action'      =>'index',
                'nav_params'      =>'',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daohanguserindex',
                'op_module'       =>'daohang',
                'op_controll'     =>'navs',
                'op_action'       =>'left',
                'op_order'        =>954,
            ],
            [
                'nav_text'        =>'呆错导航系统',
                'nav_type'        =>'link',
                'nav_url'         =>'http://www.daicuo.com',
                'nav_target'      =>'_self',
                'nav_ico'         =>'fa fa-fw fa-list',
                'nav_active'      =>'daicuo',
                'op_module'       =>'daohang',
                'op_controll'     =>'navbar',
                'op_action'       =>'footer',
                'op_order'        =>1001,
            ],
        ]);
    }
    
    //批量添加初始权限
    private function insertAuth()
    {
        \daicuo\Op::delete_all(['op_module'=>'daohang','op_controll'=>'system','op_action'=>'auth']);
        
        return \daicuo\Op::save_all([
            [
                'op_name'       => 'guest',
                'op_value'      => 'daohang/index/index',
                'op_module'     => 'daohang',
                'op_controll'   => 'system',
                'op_action'     => 'auth',
                'op_order'      => 0,
                'op_status'     => 'normal',
            ],
        ]);
    }
    
    //批量添加初始分类
    private function insertCategory()
    {
        //数据验证
        $this->insertValidate('common/Term', 'save', ['term_type'=>['eq','category']]);
        
        //添加一级分类
        $this->termSave('category', ['生活服务','休闲娱乐','教育文化','网络科技','政府组织','交通旅游','医疗健康','综合其它']);
        
        //添加二级分类
        $this->termSave('category', ['家政服务','生活百科','餐饮美食','家居建材','宠物玩具','分类信息','求职招聘','房产网站','聊天交友','汽车网站','影楼婚嫁','金融财经','银行保险','常用查询','母婴网站','驾校学车','证券网站'], '生活服务');
        
        return true;
    }
    
    //批量添加初始标签
    private function insertTag()
    {
        //数据验证
        $this->insertValidate('common/Term', 'save', ['term_type'=>['eq','category']]);
        //添加标签
        return $this->termSave('tag', ['公众号','小程序','个人微信','QQ','抖音','快手','腾讯','阿里','百度','今日头条','美团']);
    }
    
    //插入数据前（表单验证/验证场景/别名唯一值）
    private function insertValidate($validateName='', $validateScene='', $slugUnique=[]){
        //定义验证名称
        config('common.validate_name', $validateName);
        //定义验证场景
        config('common.validate_scene', $validateScene);
        //定义别名唯一值
        config('common.where_slug_unique', $slugUnique);
    }
    
    /**
     * 批量添加标签或分类
     * @version 1.0.0 首次引入
     * @param string $type 必需;队列方式(category|tag);默认：category
     * @param array $post 必需;数组格式,支持的字段列表请参考手册;默认：空
     * @param string $parentName 可选;父级名称,默认：空
     * @return mixed $mixed 成功时返回obj,失败时null
     */
    private function termSave($type='category', $post=[], $parentName='')
    {
        $default = [
            'term_name'       => '网址导航',
            'term_slug'       => 'wzdh',
            'term_module'     => 'daohang',
            'term_status'     => 'normal',
            'term_order'      => 0,
            'term_type'       => $type,
            'term_info'       => '收录网址导航相关的网站',
            'term_parent'     => 0,
            'term_tpl'        => 'index',
        ];
        $list = [];
        if($parentName){
            foreach($post as $key=>$value){
                array_push($list, DcArrayArgs([
                    'term_name' => $value,
                    'term_slug' => DcPinYin($value),
                    'term_info' => '收录与'.$value.'相关的网站',
                    'term_parent' => DcTermNameToId($parentName),
                ], $default));
            }
        }else{
            foreach($post as $key=>$value){
                array_push($list, DcArrayArgs([
                    'term_name' => $value,
                    'term_slug' => DcPinYin($value),
                    'term_info' => '收录与'.$value.'相关的网站',
                ], $default));
            }
        }
        return \daicuo\Term::save_all($list);
    }
	
}